import { createRouter, createWebHashHistory } from "vue-router";
import Home from "../views/Home/Home.vue";

import MeetingPage from "@/views/Meeting/MeetingPage.vue";

import ProfilePage from "@/views/Profile/ProfilePage.vue";

import MessagePage from "@/views/Message/MessagePage.vue";

import ProfileSettings from "@/views/ProfileSetting/ProfileSettings.vue";

import SettingsPage from "@/views/ProfileSetting/SettingsPage.vue";

import CreatePost from "@/views/Profile/CreatePost.vue";

import VerificationCodePage from "@/components/login/VerificationCodePage.vue";
import MobileLoginPage from "@/components/login/MobileLoginPage.vue";
import Content from "@/components/myContent/Content.vue";

import LoginPage from "@/components/login/LoginPage.vue";

import UserProfilePagePreview from "@/components/myContent/UserProfilePagePreview.vue";

import ChatInterface from "@/views/Message/ChatInterface.vue";

import MemberCenter from "@/views/member/MemberCenter.vue";

import PaymentPage from "@/views/member/PaymentPage.vue";

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      name: "home",
      component: Home,
      meta: { requiresAuth: true },
    },
    {
      path: "/login",
      name: "login",
      component: LoginPage,
    },
    {
      path: "/login/sms",
      name: "LoginSms",
      component: MobileLoginPage,
    },
    {
      path: "/content",
      name: "content",
      component: Content,
      meta: { requiresAuth: true },
    },
    {
      path: "/member",
      name: "member",
      component: MemberCenter,
    },
    {
      path: "/pay",
      name: "payPage",
      component: PaymentPage,
    },
    {
      path: "/content/preview",
      name: "preview",
      component: UserProfilePagePreview,
      meta: { requiresAuth: true },
    },
    {
      path: "/login/verification",
      name: "verification",
      component: VerificationCodePage,
    },
    {
      path: "/Meeting",
      name: "MeetingPage",
      component: MeetingPage,
      meta: { requiresAuth: true },
    },
    {
      path: "/chatbox",
      name: "chatbox",
      component: ChatInterface,
      meta: { requiresAuth: true },
    },
    {
      path: "/Profile",
      name: "ProfilePage",
      component: ProfilePage,
      meta: { requiresAuth: true },
    },
    {
      path: "/Message",
      name: "MessagePage",
      component: MessagePage,
      meta: { requiresAuth: true },
    },
    {
      path: "/Mine",
      name: "ProfileSettings",
      component: ProfileSettings,
      meta: { requiresAuth: true },
    },
    {
      path: "/Share",
      name: "CreatePost",
      component: CreatePost,
      meta: { requiresAuth: true },
    },
    {
      path: "/settings",
      name: "Settings",
      component: SettingsPage,
      meta: { requiresAuth: true },
    },
  ],
});

// 全局路由守卫
router.beforeEach(async (to) => {
  // 在回调内部动态导入
  const { useUserStore } = await import("@/stores/user");
  const userStore = useUserStore();

  // 开发环境跳过守卫
  // if (process.env.NODE_ENV === "development") {
  //   return true;
  // }

  // 登录相关页面直接放行
  if (to.path.startsWith("/login")) {
    return true;
  }

  // 检查是否需要认证
  if (to.meta.requiresAuth) {
    // 如果没有token，重定向到登录页
    if (!userStore.token) {
      return {
        path: "/login",
        query: { redirect: to.fullPath },
      };
    }
  }

  return true;
});

export default router;
